﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;

namespace Passwords.Biz
{
    public class PasswordItem : Entity
    {
        protected string value;
        protected string undo;
        private PasswordLabel label;

        public PasswordItem(int id, Password password, int labelId, string value)
            : base(id, "")
        {
            if (password == null) throw new ArgumentNullException("password");

            this.Password = password;
            this.LabelId = labelId;
            this.undo = value;
            this.value = value;
            IsEmpty = false;
        }

        public PasswordItem(Password password, PasswordLabel label)
            : base()
        {
            if (password == null) throw new ArgumentNullException("password");
            if (label == null) throw new ArgumentNullException("label");
            IsEmpty = true;
            this.Password = password;
            this.label = label;
            this.LabelId = label != null ? label.Id : 0;
        }


        public new bool IsModified { get { return undo != value; } }

        public virtual bool IsReadonly { get { return false; } }

        //public bool IsEmpty { get; protected set; }
        public bool IsEmpty { 
            get { return string.IsNullOrEmpty(value); }
            set { }
        }

        public bool IsPassword
        {
            get
            {
                return ValueType == ItemType.Password || ValueType == ItemType.NumericPassword;
            }
        }

        public int LabelId { get; private set; }

        public override string Name
        {
            get
            {
                return Label.Name;
            }
            set { throw new NotSupportedException(); }
        }

        public PasswordLabel Label
        {
            get
            {
                if (label == null) label = Context.GetLabel(LabelId);
                return label;
            }
        }

        public Password Password { get; private set; }

        public ItemType ValueType { get { return Label.ValueType; } }

        public int DisplayIndex { get { return Label.DisplayIndex; } }

        public virtual string Value
        {
            get { return value ?? string.Empty; }
            set
            {
                this.value = value;
                OnPropertyChanged("Value");
            }
        }

        public override void OnPropertyChanged(string propertyName)
        {
            IsEmpty = false;
            base.OnPropertyChanged(propertyName);
        }

        public virtual void Undo()
        {
            value = undo;
            EntityState = EntityState.Unchanged;
        }

        public virtual void Save()
        {
            undo = value;
            if (EntityState == EntityState.Modified && Password != null) Password.OnPropertyChanged("Items");
        }


        public void MakeEmpty()
        {
            Value = string.Empty;
            Save();
            IsEmpty = true;
        }
    }
}
